<?php
/**
 * 
 * 购物车信息
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-4-17
 * model() 创建模型对象，静态
 * tableName() 返回表名
*/

class Cart extends CActiveRecord{
	/**
	 *返回当前模型对象的方法 
	 */
	public static function model($className = __CLASS__){
		return parent::model($className);
	}
	
	/**
	 * 返回当前表名
	 */
	public function tableName(){
		return '{{cart}}';
	}
	
	
	
	/**
	 * 往购物车添加商品
	 */
	public function addProduct($user_id,$goods_id,$nums = 1){
		$cmodel = new CCCModel();
		$result = $cmodel->sel("SELECT * FROM {{cart}} WHERE user_id=$user_id AND goods_id = $goods_id ");
		if(empty($result)){
			$_sql = "INSERT INTO {{cart}}(`user_id`,`goods_id`,`cart_goods_count`,`cart_time`) VALUES($user_id,$goods_id,$nums,".time().")";
		}else{
			$_sql = "UPDATE  {{cart}} set cart_goods_count = cart_goods_count + 1,active=1 WHERE user_id = $user_id AND goods_id =  $goods_id";
		}
		$row = $cmodel -> upd($_sql);
		return $row;
	}
	
	/**
	 * 购物车数量更新
	 */
	public function updProduct($user_id,$goods_id){
		$cmodel = new CCCModel();
		$_sql = "UPDATE  {{cart}} set cart_goods_count = cart_goods_count+1,active=1 WHERE user_id=$user_id AND goods_id =  $goods_id";
		$row = $cmodel -> upd($_sql);
		return $row;
	}
	
	/**
	 * 根据用户id查询购物车
	 */
	public function selCartByuserId($user_id){
		$cmodel = new CCCModel();
		$_sql = "SELECT b.goods_id,b.goods_thumbnail_url,b.goods_name,b.brand_id,b.goods_price,a.cart_goods_count,a.active FROM {{cart}} a,{{goods}} b WHERE a.active=1 AND   a.goods_id=b.goods_id AND a.user_id= $user_id";
		$info = $cmodel -> sel($_sql);
		return $info;
	}
	
	/**
	 * 通过用户和商品id确定商品(判断是否已经存在)
	 */
	
	public function selUserGoods($user_id,$goods_id){
		$cmodel = new CCCModel();
		$_sql = "SELECT * FROM {{cart}} WHERE user_id=$user_id AND goods_id=$goods_id";
		$info = $cmodel -> sel($_sql);
		return $info;
	}
	
	/**
	 * 移出购物车
	 */
	public function delcart($goods_id,$user_id){
		$cmodel = new CCCModel();
		$_sql = "UPDATE  {{cart}} set active = 0,cart_goods_count=1 WHERE user_id=$user_id AND goods_id =  $goods_id";
		$info = $cmodel -> upd($_sql);
		return $info;
	}
	
	/**
	 * 购物车更新
	 */
	public function updcart($user_id,$goods_id,$nums){
		$cmodel = new CCCModel();
		$_sql = "UPDATE  {{cart}} set cart_goods_count = $nums WHERE user_id=$user_id AND goods_id =  $goods_id";
		$info = $cmodel -> upd($_sql);
		return $info;
	}
	
	/**
	 * 是否有效的更新
	 */
	public function updcartactive($user_id,$goods_id){
			$cmodel = new CCCModel();
			$_sql = "UPDATE {{cart}} SET active = 0 WHERE user_id=".$user_id;
			$info = $cmodel -> upd($_sql);
			foreach ($goods_id as $k=>$v){ 			
				$_sql = "UPDATE {{cart}} SET active = 1 WHERE user_id=".$user_id." AND goods_id=".$v;
				$info = $cmodel -> upd($_sql);
			} 	
	}

	/**
	 *根据用户id查询购物车共有多少件商品
	 */
	public function getCartTotalCountByUser_id($user_id){
		
		$cmodel = new CCCModel();
		$_sql = "SELECT sum(cart_goods_count) AS count_total FROM {{cart}} WHERE user_id=$user_id";
		$row = $cmodel->sel($_sql);
		return $row;
	}
	
	/**
	 * 获得用户已经勾选的商品
	 */
	public function GetActiveGoods($user_id){
			$cmodel = new CCCModel();
			$_sql = "SELECT * FROM {{cart}} a,{{goods}} b WHERE a.goods_id=b.goods_id AND  a.active=1 AND a.user_id=$user_id";
			$info = $cmodel -> sel($_sql);
			return $info;
	}

}
	
?>